package com.lzh.excel.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;

import java.time.YearMonth;
import java.time.format.DateTimeFormatter;

/**
 * @author zhehen.lu
 * @date 2025/8/3 14:26
 */
public class EasyExcelYearMonthConverter implements Converter<YearMonth> {
    public EasyExcelYearMonthConverter() {
    }

    @Override
    public Class<YearMonth> supportJavaTypeKey() {
        return YearMonth.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public YearMonth convertToJavaData(ReadConverterContext<?> context) throws Exception {
        return null;
    }

    @Override
    public WriteCellData<String> convertToExcelData(WriteConverterContext<YearMonth> context) throws Exception {
        return new WriteCellData(((YearMonth)context.getValue()).format(DateTimeFormatter.ofPattern("yyyy-MM")));
    }
}
